package com.bawei.hostapp.plugincore;

import android.content.Context;
import android.content.Intent;
import android.util.Log;

import com.bawei.hostapp.RegisteredActivity;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

/**
 * @Author : yaotianxue
 * @Time : On 2023/6/6 08:27
 * @Description : AMSInvocationHandler
 */
public class AMSInvocationHandler implements InvocationHandler {
    private Context mContext;
    private Object subject;

    public AMSInvocationHandler(Context context, Object subject) {
        mContext = context;
        this.subject = subject;
    }
    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        //拦截startActivity方法
        if("startActivity".equals(method.getName())) {
            Log.d("ytx","Proxy IActivityTaskManager startActivity invoke...");
            for (int i = 0; i < args.length; i++) {
                if (args[i] instanceof Intent) {
                    Intent intent = new Intent();
                    intent.setClass(mContext, RegisteredActivity.class);
                    intent.putExtra("actionIntent", (Intent) args[i]);
                    args[i] = intent;
                    Log.d("ytx","replaced startActivity intent");
                }
            }
        }
        return method.invoke(subject,args);
    }
}
